package com.zhengxyou.jetpackdemo.lifecycle_liveData_viewModel.view_model;

import android.content.Context;

import java.util.List;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Transformations;
import androidx.lifecycle.ViewModel;

public class NameViewModel extends ViewModel {
    private MutableLiveData<String> mCurrentName = new MutableLiveData<>();
    private MutableLiveData<List<String>> mCurrentListName = new MutableLiveData<>();

    private LiveData<String> mTrans = Transformations.map(mCurrentName, input -> input + "input");
    private LiveData<String> mSwitchMap = Transformations.switchMap(mCurrentName, this::getSwitchLiveData);
    private LiveData<String> distinctUntilChanged = Transformations.distinctUntilChanged(mCurrentName);


    public LiveData<String> getDistinctUntilChanged() {
        return distinctUntilChanged;
    }

    private LiveData<String> getSwitchLiveData(String input) {
        return new MutableLiveData<>(input + "switch");
    }

    public MyLiveData getWifiLevel(Context context) {
        return MyLiveData.getInstance(context);
    }

    public LiveData<String> getSwitchTrans() {
        return mSwitchMap;
    }

    public LiveData<String> getTrans() {
        return mTrans;
    }

    public MutableLiveData<String> getCurrentName() {
        if (mCurrentName == null) {
            mCurrentName = new MutableLiveData<>();
        }
        return mCurrentName;
    }

    public MutableLiveData<List<String>> getCurrentListName() {
        if (mCurrentListName == null) {
            mCurrentListName = new MutableLiveData<>();
        }
        return mCurrentListName;
    }
}
